package com.ptswitch.material.pages.user;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.common.EMailAddress;
import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.EMailAddressService;
import com.ptswitch.material.service.UserService;

public class ListUser extends MaterialBasePage {
    @Inject
    private UserService userSer;
    @Inject
    private EMailAddressService emailAddressSer;

    @Property
    private List<Uzer> list;
    @Property
    private List<EMailAddress> emailAddresses;
    @Property
    private EMailAddress emailAddress;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        checkUserIsAdmin();
        emailAddress = new EMailAddress();
        return null;
    }

    @SetupRender
    Object display() {
        checkUserIsAdmin();
        list = userSer.findAll();
        emailAddresses = emailAddressSer.findAll();
        return null;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "addAddress")
    Object addAddress() {
        checkUserIsAdmin();
        emailAddressSer.save(emailAddress);
        return null;
    }
}
